//set working directory 
cd  "C:\Users\johna\Dropbox (Curiel Analytx)\ElectionNightResults\replication_code"
//the above is just the path to the replcation files. The data from here on are 
//hierarchical and nested from this file path. 
use "data\nyt_counties.dta", clear
sort state name time


** calculate the % of ballots cast by mail

egen max_absentee_votes = max(absentee_votes),by(state name)
replace max_absentee_votes = . if max_absentee_votes == 0
gen absentee_votespct = max_absentee_votes / final_votes

** calculate which candidate won each county

gen bidenpct = 100 * max_bidenj / (max_bidenj + max_trumpd)

//hours from var 
gen hour4 = hours_from < 4 & hours_from[_n+1] > 4
gen hour8 = hours_from < 8 & hours_from[_n+1] > 8



levelsof state, local(states)

gen corr_final8 = .
gen corr_bidenpct8 = .
gen corr_absentee_votespct8 = .
gen corr_final4 = .
gen corr_bidenpct4 = .
gen corr_absentee_votespct4 = .
gen state2 = ""
local row = 0
foreach s of local states {
    display "`s'"
    local row = `row' + 1
	quietly replace state2 = "`s'" in `row'
    quietly corr pct_reported final if pct_reported < 101 & state == "`s'" & hour4 == 1
	quietly replace corr_final4 = r(rho) in `row'
	quietly corr pct_reported bidenpct if pct_reported < 101 & state == "`s'" & hour4 == 1
	quietly replace corr_bidenpct4 = r(rho) in `row'
	quietly capture corr pct_reported absentee_votespct if pct_reported < 101 & state == "`s'" & hour4 == 1
	quietly replace corr_absentee_votespct4 = r(rho) in `row'
    quietly corr pct_reported final if pct_reported < 101 & state == "`s'" & hour8 == 1
	quietly replace corr_final8 = r(rho) in `row'
	quietly corr pct_reported bidenpct if pct_reported < 101 & state == "`s'" & hour8 == 1
	quietly replace corr_bidenpct8 = r(rho) in `row'
	quietly capture corr pct_reported absentee_votespct if pct_reported < 101 & state == "`s'" & hour8 == 1
	quietly replace corr_absentee_votespct8 = r(rho) in `row'
}

keep corr* state2 *8 *4
rename state2 state
drop in 52/l


** Graph correlation with final_votes

gen zero = 0
compress
replace corr_final8 = -1 if state == "District of Columbia" | state == "Hawaii"
sort corr_final8 state
replace corr_final8 = . if state == "District of Columbia" | state == "Hawaii"
gen order = _n

twoway (pcspike order zero order corr_final8, lcolor(gray) lwidth(vthick)) ///
       (scatter order corr_final8, mcolor(sand) msize(medium) msymbol(circle)) ///
	   (scatter order zero if corr_final8  > 0, msymbol(none) mlabel(state) mlabsize(6-pt) mlabposition(9)) ///
	   (scatter order zero if corr_final8  < 0, msymbol(none) mlabel(state) mlabsize(6-pt) mlabposition(3)) ///
	   , ///
	   ytitle("") ///
	   ylabel(none) ///
	   xtitle(rho) ///
	   xmtick(-1(.1).6, nogrid) ///
	   title(Correlation with number of ballots cast) ///
	   legend(off) ///
	   xsize(4) ///
	   ysize(6)
graph export "figures\corr_final8.png", replace width(2500)
	   

** Graph correlation with Biden %

drop order
compress
replace corr_bidenpct8 = -1 if state == "District of Columbia" | state == "Hawaii"
sort corr_bidenpct8 state
gen order = _n
replace corr_bidenpct8 = . if state == "District of Columbia" | state == "Hawaii"

twoway (pcspike order zero order corr_bidenpct8, lcolor(gray) lwidth(vthick)) ///
       (scatter order corr_bidenpct8, mcolor(sand) msize(medium) msymbol(circle)) ///
	   (scatter order zero if corr_bidenpct8  > 0, msymbol(none) mlabel(state) mlabsize(6-pt) mlabposition(9)) ///
	   (scatter order zero if corr_bidenpct8  < 0, msymbol(none) mlabel(state) mlabsize(6-pt) mlabposition(3)) ///
	   , ///
	   ytitle("") ///
	   ylabel(none) ///
	   xtitle(rho) ///
	   xmtick(-1(.1).6, nogrid) ///
	   title(Correlation with Biden two-party vote share) ///
	   legend(off) ///
	   xsize(4) ///
	   ysize(6)	   
graph export "figures\corr_bidenpct8.png", replace width(2500)
	   

** Graph correlation with absentee %

drop order
compress
replace corr_absentee_votespct8 = -1 if state == "District of Columbia" | state == "Hawaii" | corr_absentee_votespct8 == .
sort corr_absentee_votespct8 state
replace corr_absentee_votespct8 = . if state == "District of Columbia" | state == "Hawaii" | corr_absentee_votespct8 == -1
gen order = _n

twoway (pcspike order zero order corr_absentee_votespct8, lcolor(gray) lwidth(vthick)) ///
       (scatter order corr_absentee_votespct8, mcolor(sand) msize(medium) msymbol(circle)) ///
	   (scatter order zero if corr_absentee_votespct8  > 0, msymbol(none) mlabel(state) mlabsize(6-pt) mlabposition(9)) ///
	   (scatter order zero if corr_absentee_votespct8  < 0, msymbol(none) mlabel(state) mlabsize(6-pt) mlabposition(3)) ///
	   , ///
	   ytitle("") ///
	   ylabel(none) ///
	   xtitle(rho) ///
	   xmtick(-1(.1).6, nogrid) ///
	   title(Correlation with absentee ballot pct.) ///
	   legend(off) ///
	   xsize(4) ///
	   ysize(6)	   	   
graph export "figures\corr_absentee_votespct8.png", replace width(2500)
	   